package com.hpin.fansea.sale;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.adapter.GoodsCommentAdapter;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.GoodsCommentBean;
import com.hpin.fansea.pulltorefush.PullToRefreshBase;
import com.hpin.fansea.pulltorefush.PullToRefreshListView;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;
import com.hpin.fansea.widget.CircleImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Aaron on 15/10/22.
 */
public class PurchaseCommentActivity extends BaseActivity {

    private String purchaseId;
    public DisplayImageOptions options;

    private int currentIndex;
    private PullToRefreshListView lv_pirchase_comment;

    private List<GoodsCommentBean.GoodsComRes> goodsComRests = new ArrayList<>();
    private GoodsCommentAdapter commentAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_goods_comment);

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.icon_headpic_default)
                .showImageForEmptyUri(R.mipmap.icon_headpic_default)
                .showImageOnFail(R.mipmap.icon_headpic_default)
                .resetViewBeforeLoading(true)
                .cacheOnDisk(true).cacheInMemory(true)
                .cacheOnDisc(true).imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .considerExifParams(true)
                .displayer(new SimpleBitmapDisplayer()).build();

        purchaseId = getIntent().getStringExtra("purchaseId");

        initWidget();

//        getCommentData();

    }

    @Override
    protected void onResume() {
        super.onResume();

        getCommentData();

    }

    private void getCommentData() {

        HttpUtil.getInstance().setGetRequest("获取中...", this, Constant.WEB_ROOT + Constant.GOODSCOMMENT
                        + "/" + purchaseId + "/reviewList" + "?token=" + sp.getString(Constant.token, "")
                        + "&pageSize=" + Constant.PAGECOUNT + "&pageIndex=" + (++currentIndex)
                        + "&systemType=" + Constant.SYSTEM_TYPE, new HttpUtil.SuccessListener() {

                    @Override
                    public void setOnSuccessListener(String str) {

                        lv_pirchase_comment.onRefreshComplete();

                        try {

                            GoodsCommentBean commentBean = JSONObject.parseObject(str, GoodsCommentBean.class);

                            List<GoodsCommentBean.GoodsComRes> goodsComRes = commentBean.result;

                            setListView(goodsComRes);

                            commentAdapter.setDate(goodsComRests);
                            commentAdapter.notifyDataSetChanged();

                        } catch (Exception e) {
                            e.printStackTrace();
                            showToast(Constant.NODATA);
                        }

                    }
                }, new HttpUtil.FailListener() {
                    @Override
                    public void setOnFailListener() {

                        lv_pirchase_comment.onRefreshComplete();

                    }
                }
        );

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);

        iv_left.setOnClickListener(this);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("评论");

        CircleImageView ci_head_pic = (CircleImageView) findViewById(R.id.ci_head_pic);

        ImageLoader.getInstance().displayImage(sp.getString(Constant.headPic, ""), ci_head_pic, options);

        RelativeLayout rl_send_my_commemt = (RelativeLayout) findViewById(R.id.rl_send_my_commemt);

        rl_send_my_commemt.setOnClickListener(this);

        lv_pirchase_comment = (PullToRefreshListView) findViewById(R.id.lv_pirchase_comment);

        commentAdapter = new GoodsCommentAdapter(this, goodsComRests);

        lv_pirchase_comment.setAdapter(commentAdapter);

        lv_pirchase_comment.setMode(PullToRefreshBase.Mode.BOTH);

        lv_pirchase_comment.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                String label = DateUtils.formatDateTime(PurchaseCommentActivity.this, System.currentTimeMillis(),
                        DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);

                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

                currentIndex = 0;
                goodsComRests.clear();
                getCommentData();

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

                String label = DateUtils.formatDateTime(PurchaseCommentActivity.this, System.currentTimeMillis(),
                        DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);

                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

                getCommentData();
            }
        });

    }

    /**
     * 判断是否有重复并更新
     */
    private void setListView(List<GoodsCommentBean.GoodsComRes> tempGuideLists) {

        if (tempGuideLists == null || tempGuideLists.size() == 0)
            return;

        if (goodsComRests.size() == 0) {

            goodsComRests.addAll(tempGuideLists);
            return;
        }

        for (GoodsCommentBean.GoodsComRes goods : tempGuideLists) {
            boolean exist = false;
            for (GoodsCommentBean.GoodsComRes g : goodsComRests) {
                if (goods.id_.equals(g.id_)) {
                    exist = true;
                    break;
                }
            }
            if (!exist)
                goodsComRests.add(goods);
        }

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:

                finish();

                break;

            case R.id.rl_send_my_commemt:
                //发表评论

                Intent mIntent = new Intent(this, GoodsMyCommentActivity.class);
                mIntent.putExtra("groupId", purchaseId);
                startActivity(mIntent);

                break;

        }
    }
}
